<cfset var.start = CreateODBCdateTime(now())>
<cfif not isdefined('request.dsn')>
	<cflock timeout="15" throwontimeout="No" scope="Application" type="Exclusive">
		<cfset request.dsn = application.dsn>
	</cflock>
</cfif>

<cfsetting enablecfoutputonly="yes">
<cfparam name="attributes.url" default="#application.xtiurl#">
<cfparam name="attributes.dtd" default="#application.xtidtd#">
<cfparam name="attributes.debug" default="false">
<cfparam name="attributes.debuglevel" default="1,2,3">
<cfparam name="attributes.customerid">
<cfparam name="attributes.contactid" default="">
<cfparam name="attributes.crt_order" default="">
<cfparam name="attributes.order_number" default="">

<!--- ORDER HEADER --->
<cfparam name="attributes.misc_code_1" default="">
<cfparam name="attributes.misc_amt_1" default="">
<cfparam name="attributes.misc_code_2" default="">
<cfparam name="attributes.misc_amt_2" default="">
<cfparam name="attributes.source">
<cfparam name="attributes.ship_complete" default="">
<cfparam name="attributes.tsr_taker">
<cfparam name="attributes.user" default="web">
<cfparam name="attributes.locid">
<cfparam name="attributes.cid" default="">
<cfparam name="attributes.client_order" default="">
<cfparam name="attributes.order_type" default="R">
<cfparam name="attributes.program_id" default="">
<cfparam name="attributes.confidence" default="">
<cfparam name="attributes.close_date" default="">
<cfparam name="attributes.close_amt" default="">

<!--- PAYMENT TYPE --->
<cfif attributes.payment_type is "v" or attributes.payment_type is "m" or attributes.payment_type is "d">
	<cfparam name="attributes.payment_type" default="Credit_Card">
<cfelse>
	<cfparam name="attributes.payment_type">
</cfif>
<cfparam name="attributes.card_number" default="">
<cfparam name="attributes.security_code" default="">
<cfparam name="attributes.expr_date" default="">
<cfif attributes.payment_type is "a">
	<cfparam name="attributes.amex_num" default="">
<cfelse>
	<cfparam name="attributes.amex_num" default="">
</cfif>
<cfparam name="attributes.ponumber" default="">
<cfparam name="attributes.ccln" default="">
<cfparam name="attributes.partial_ccn" default="">
<cfparam name="attributes.amount" default="">
<cfparam name="attributes.new_card" default="">
<cfparam name="attributes.card_amount" default="">

<!--- SHIPPING PARAMS --->
<cfparam name="attributes.company_name">
<cfparam name="attributes.attention">
<cfparam name="attributes.address1">
<cfparam name="attributes.address2" default="">
<cfparam name="attributes.address3" default="">
<cfparam name="attributes.address4" default="">
<cfparam name="attributes.city">
<cfparam name="attributes.state">
<cfparam name="attributes.country">
<cfparam name="attributes.zip" default="">
<cfparam name="attributes.geo_code" default="">
<cfparam name="attributes.phone" default="">
<!---SHIPPING CODES  --->
<cfparam name="attributes.code">
<cfparam name="attributes.account_number" default="">
<cfparam name="attributes.ship_price" default="">
<cfparam name="attributes.ship_code_reason" default="">
<cfparam name="attributes.cqsd" default="">
<!--- SYSTEM NOTES --->
<cfparam name="attributes.packing_slip_notes" default="">
<cfparam name="attributes.invoice_notes" default="">
<cfparam name="attributes.management_notes" default="">
<cfparam name="attributes.auto_bo" default="">
<cfparam name="attributes.future_ship" default="">
<!--- ARRAYS SYSTEM AND LINITEM  --->
<cfparam name="attributes.array_parts">
<cfparam name="attributes.array_systems" default="">
<!--- BEGIN TO BUILD THE XML POST --->
<cfscript>
payment = "";
if  (isarray(attributes.array_ccs) and arraylen(attributes.array_ccs) GT 0) {
	indexcnt = arraylen(attributes.array_ccs) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
      payment = payment & '         
  <payment type="#attributes.pay_type#">
    <ccln>#ucase(attributes.array_ccs[i].cc_line)#</ccln>
    <partial_ccn>#ucase(attributes.array_ccs[i].partial_num)#</partial_ccn>
    <amount>#ucase(attributes.array_ccs[i].amount)#</amount>
    <expr_date>#ucase(attributes.array_ccs[i].expiration)#</expr_date>
  </payment>         
      ';
	}
}
else{
	payment = '
    <payment type="#attributes.pay_type#">
      <ccln></ccln>
      <partial_ccn></partial_ccn>
      <amount></amount>
      <expr_date></expr_date>
	  <po_number>#attributes.ponumber#</po_number>
     </payment>         
      ';
}

new_payment="";
if (attributes.new_card IS "yes") {
	new_payment = '
	<payment type="credit_card">
         <card_number>#attributes.card_number#</card_number>
         <expr_date>#attributes.expr_date#</expr_date>
         <amex_num></amex_num>
         <po_number></po_number>
         <amount>#attributes.card_amount#</amount>
		 <security_code>#attributes.security_code#</security_code>
     </payment>	
	';
}

cart = "";
subtotal = 0;
// Start with standalone parts
if  (isarray(attributes.array_parts) and arraylen(attributes.array_parts) GT 0) {
	indexcnt = arraylen(attributes.array_parts) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
      cart = cart & '
         <line_item>
            <product>				
               <product_code>#ucase(attributes.array_parts[i].product)#</product_code>';
			   if (attributes.array_parts[i].type IS "original"){
               cart = cart & '
               <ln>#ucase(attributes.array_parts[i].line)#</ln>';
               }
			   if (IsDefined("application.license_subscription") AND application.license_subscription EQ '1'){
               cart = cart & '
               <reoccurrence>#Trim(attributes.array_parts[i].number_reoccurrences)#</reoccurrence>
               <start_date>#DateFormat(ucase(attributes.array_parts[i].subscription_start_date), 'MMDDYY')#</start_date>
               <ori_order>#Trim(attributes.array_parts[i].ori_order)#</ori_order>';
			   }
			   cart = cart & '
               <quantity>#ucase(attributes.array_parts[i].quantity)#</quantity>
               <price>#ucase(attributes.array_parts[i].price)#</price>
			   <promo_code_1>#ucase(attributes.array_parts[i].promo_code_1)#</promo_code_1>
			   <promo_code_2>#ucase(attributes.array_parts[i].promo_code_2)#</promo_code_2>
			   <rsn_code_prc>#ucase(attributes.array_parts[i].price_reason_code)#</rsn_code_prc>
               <bid>#ucase(attributes.array_parts[i].bid)#</bid>
               <bid_exp>#ucase(attributes.array_parts[i].bid_exp)#</bid_exp>
            </product>
         </line_item>
      ';
	}
}

// Now the systems 
if  (isarray(attributes.array_systems) and arraylen(attributes.array_systems)) {
	indexcnt1 = arraylen(attributes.array_systems) + 1;  
 //writeoutput(arraylen(attributes.array_systems));
	for (x=1; x lt indexcnt1; x=x+1) {
	  cart = cart & '
         <line_item>	
             <configuration>';
                if (attributes.array_systems[x].type IS "original"){
                cart = cart & '
                 <ln>#ucase(attributes.array_systems[x].line)#</ln>';
                }		   
	             cart = cart & '
                 <quantity>#ucase(attributes.array_systems[x].quantity)#</quantity>			   
                 <baseunit>
                     <product_code>#ucase(attributes.array_systems[x].b_product)#</product_code>
                     <price>#ucase(attributes.array_systems[x].b_price)#</price>
					 <promo_code_1>#ucase(attributes.array_parts[i].promo_code_1)#</promo_code_1>
			   		 <promo_code_2>#ucase(attributes.array_parts[i].promo_code_2)#</promo_code_2>
                     <bid>#ucase(attributes.array_systems[x].bid)#</bid>
                     <bid_exp>#ucase(attributes.array_systems[x].bid_exp)#</bid_exp>
                     <rsn_code_prc>#ucase(attributes.array_systems[x].price_reason_code)#</rsn_code_prc>
                  </baseunit>';
		indexcnt2 = arraylen(attributes.array_systems[x].subitem) + 1; 					
       //writeoutput(arraylen(attributes.array_systems[x].subitem));
       for (y=1; y lt indexcnt2;  y=y+1) {
       cart = cart & '
                  <component>';
	                 cart = cart & '
                      <product_code>#ucase(attributes.array_systems[x].subitem[y].product)#</product_code>
                      <quantity>#ucase(attributes.array_systems[x].subitem[y].quantity)#</quantity>
                      <price>#ucase(attributes.array_systems[x].subitem[y].price)#</price>
                      <rsn_code_prc>#ucase(attributes.array_systems[x].subitem[y].price_reason_code)#</rsn_code_prc>
                  </component>';
		}
		cart = cart & '            
             </configuration>				
         </line_item>';
		 }	
//	}
} 

// Now build the rest of the xml to post to middle ware. KEEP FORMATING SAME AS BELOW...
xml = '<!DOCTYPE copy SYSTEM "#attributes.dtd#">
<xtiinput>
   <inputheader>
      <actionset>
         <action>chg_order</action>
      </actionset>
   </inputheader>
   <inputdata>
      <customerid>#ucase(attributes.customerid)#</customerid>
      <contactid>#ucase(attributes.contactid)#</contactid>
      <order_number>#ucase(attributes.order_number)#</order_number>
      <order_header>
         <misc_code_1>#ucase(attributes.misc_code_1)#</misc_code_1>
         <misc_amt_1>#ucase(attributes.misc_amt_1)#</misc_amt_1>
         <misc_code_2>#ucase(attributes.misc_code_2)#</misc_code_2>
         <misc_amt_2>#ucase(attributes.misc_amt_2)#</misc_amt_2>
         <source>#ucase(attributes.source)#</source>
         <ship_complete>#ucase(attributes.ship_complete)#</ship_complete>
         <tsr_taker>#ucase(attributes.tsr_taker)#</tsr_taker>
         <user>#ucase(attributes.user)#</user>
         <client_order>#ucase(attributes.client_order)#</client_order>
         <order_type>#xmlformat(ucase(attributes.order_type))#</order_type>
         <program_id>#attributes.program_id#</program_id>
         <locid>#attributes.locid#</locid>
         <cid>#attributes.cid#</cid>
         <pay_type>#attributes.payment_type#</pay_type>
         <confidence>#attributes.confidence#</confidence>
         <close_date>#attributes.close_date#</close_date>
         <close_amt>#attributes.close_amt#</close_amt>
         <auto_bo>#ucase(attributes.auto_bo)#</auto_bo>
         <future_ship_date>#ucase(attributes.future_ship)#</future_ship_date>
         <cqsd>#ucase(attributes.cqsd)#</cqsd>
      </order_header>
      #payment#
	  #new_payment#
      <shipping_addr>
         <company_name>#replace(xmlformat(ucase(attributes.company_name)), '&amp;AMP;', '&amp;', 'all')#</company_name>
         <attention>#replace(xmlformat(ucase(attributes.attention)), '&amp;AMP;', '&amp;', 'all')#</attention>
         <address_1>#replace(xmlformat(ucase(attributes.address1)), '&amp;AMP;', '&amp;', 'all')#</address_1>
         <address_2>#replace(xmlformat(ucase(attributes.address2)), '&amp;AMP;', '&amp;', 'all')#</address_2>
         <city>#ucase(attributes.city)#</city>
         <state>#ucase(attributes.state)#</state>
         <country>#ucase(attributes.country)#</country>
         <zip>#ucase(attributes.zip)#</zip>
         <geo_code>#attributes.geo_code#</geo_code>
         <phone>#ucase(attributes.phone)#</phone>
      </shipping_addr>
      <shipping_codes>
         <method>
            <code>#ucase(attributes.code)#</code>
            <account_number>#ucase(attributes.account_number)#</account_number>
            <price>#ucase(attributes.ship_price)#</price>
            <rsn_code_ship>#ucase(trim(attributes.ship_code_reason))#</rsn_code_ship>
         </method>
      </shipping_codes>
      <system_notes>
         <packing_slip_notes>#xmlformat(ucase(attributes.packing_slip_notes))#</packing_slip_notes>
         <invoice_notes>#xmlformat(ucase(attributes.invoice_notes))#</invoice_notes>
         <management_notes>#xmlformat(ucase(attributes.management_notes))#</management_notes>
      </system_notes>
      <shopping_cart>
	  #cart#
      </shopping_cart>
   </inputdata>
</xtiinput>';
</cfscript>